//
//  main.cpp
//  testpcap
//
//  Created by bugforever on 1/10/15.
//  Copyright (c) 2015 bugforever. All rights reserved.
//

#include "stdio.h"
#include <iostream>
#include "time.h"

#include "../../../../Include/network_struct.h"
#include "../../../../Common/device_retriever/device_retriever.h"
#include "../../../../Common/pcap_engine/pcap_engine_libpcap.h"

class network_filter_test
:public network_filter_base
{
public:
    network_filter_test()
    :network_filter_base( FAD_ETHERNET , FAD_ETHERNET )
    {
        ;
    }
    
    virtual ~network_filter_test(){}
    
    virtual void process_network_data( FILTER_ACCEPT_DATATYPE type , void * ptr_data )
    {
        if( type == FAD_ETHERNET )
        {
            network_ethernet_data * ptr_ethernet_data = ( network_ethernet_data * )ptr_data;
            
            char timestr[16];
            tm * ptr_time = localtime( &(ptr_ethernet_data->time) );
            strftime(timestr, sizeof timestr, "%H:%M:%S", ptr_time);
            
            printf( "%s\tSrc: %02x:%02x:%02x:%02x:%02x:%02x   Dst: %02x:%02x:%02x:%02x:%02x:%02x   Size: %d\tType: %04x\n" ,
                   timestr ,
                   ptr_ethernet_data->ether_shost[0] ,
                   ptr_ethernet_data->ether_shost[1] ,
                   ptr_ethernet_data->ether_shost[2] ,
                   ptr_ethernet_data->ether_shost[3] ,
                   ptr_ethernet_data->ether_shost[4] ,
                   ptr_ethernet_data->ether_shost[5] ,
                   ptr_ethernet_data->ether_dhost[0] ,
                   ptr_ethernet_data->ether_dhost[1] ,
                   ptr_ethernet_data->ether_dhost[2] ,
                   ptr_ethernet_data->ether_dhost[3] ,
                   ptr_ethernet_data->ether_dhost[4] ,
                   ptr_ethernet_data->ether_dhost[5] ,
                   ptr_ethernet_data->data_size ,
                   ntohs(ptr_ethernet_data->ether_type) );
            
            send_network_data(type, ptr_data);
        }
        else
        {
            std::cout<<"Error!!!!!!We should not recv this kind of data!!!!!"<<std::endl;
        }
    }
};


int main(int argc, const char * argv[])
{
    device_retriever retriever;
    std::vector<device_info *> * ptr_info = 0;
    
    bool ret = retriever.get_device_list( &ptr_info );
    
    if( !ret )
        return 0;
    
    std::vector<device_info *>::iterator iter = ptr_info->begin();
    std::vector<device_info *>::iterator itere = ptr_info->end();
    
    int index = 0;
    
    while( iter != itere )
    {
        std::cout<<index<<". Name: "<<(*iter)->get_name()<<std::endl;
        
        index++;
        iter++;
    }
    
    std::cout<<"Please select the device you want to do the capture: ";
    
    std::cin>>index;
    
    iter = ptr_info->begin();
    
    iter = iter + index;
    
    pcap_engine_libpcap engine;
    network_filter_test test;
    
    engine.set_network_filter( &test );
    
    engine.start_engine( *iter );
    
    std::cin>>index;
    
    engine.stop_engine();
    
    return 0;
}
